package cn.lizemin.toExcel;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.BorderStyle;

import java.io.OutputStream;
import java.util.Collection;
import java.util.List;

/**
 * @author: lizemin
 * @version: 1.0
 */
public class MyUtil {
    public void exportExcel(String title, String[] headers, Collection<T> dataSet, OutputStream out,String pattern){
        HSSFWorkbook workbook = new HSSFWorkbook();

        HSSFSheet sheet = workbook.createSheet();

        sheet.setDefaultColumnWidth(15);

        HSSFCellStyle style = workbook.createCellStyle();

        style.setBorderBottom(BorderStyle.THICK);

        HSSFFont font = workbook.createFont();
//        font.setColor(HSSFColor.toHSSFColor());

    }

    public static HSSFWorkbook export_text(List<Student> list){
        try {
            //创建工作薄对象
            HSSFWorkbook wb = new HSSFWorkbook();
            //创建标题行样式
            HSSFCellStyle headStyle = headStyle(wb);
            //创建内容行样式
            HSSFCellStyle contentStyle = contentStyle(wb);

            //创建表
            HSSFSheet sheet_1 =  wb.createSheet("人员信息");
            //设置表的默认列宽
            sheet_1.setDefaultColumnWidth(30);

            //创建标题行
            HSSFRow headRow = sheet_1.createRow(0);
            HSSFCell head_cell_1 = headRow.createCell(0);           //创建标题行第一列
            head_cell_1.setCellValue("姓名");                        //第一列内容
            head_cell_1.setCellStyle(headStyle);                    //将标题行样式添加

            HSSFCell head_cell_2 = headRow.createCell(1);
            head_cell_2.setCellValue("年龄");
            head_cell_2.setCellStyle(headStyle);

            //为内容行添加数据和样式
            for (int i = 1; i <= list.size(); i++) {
                HSSFRow contentRow = sheet_1.createRow(i);
                HSSFCell content_cell_1 = contentRow.createCell(0);
                content_cell_1.setCellValue(list.get(i-1).getName());
                content_cell_1.setCellStyle(contentStyle);

                HSSFCell content_cell_2 = contentRow.createCell(1);
                content_cell_2.setCellValue(list.get(i-1).getAge());
                content_cell_2.setCellStyle(contentStyle);

            }
            return wb;
        } catch (Exception e) {
            e.getStackTrace();
        }

        return null;
    }


    /**
     * 创建标题行样式
     * @param wb
     * @return
     */
    public static HSSFCellStyle headStyle(HSSFWorkbook wb){
        HSSFCellStyle headStyle = wb.createCellStyle();                       //创建样式对象
        HSSFFont headFont = wb.createFont();                                  //创建字体
        headFont.setFontName("微软雅黑");
        headFont.setColor(HSSFFont.COLOR_RED);

//        headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        headStyle.setFont(headFont);
        return headStyle;
    }

    /**
     * 创建内容行样式
     * @param wb
     * @return
     */
    public static HSSFCellStyle contentStyle(HSSFWorkbook wb){
        HSSFCellStyle contentStyle = wb.createCellStyle();
        HSSFFont contentFont = wb.createFont();
        contentFont.setFontName("微软雅黑");
//        contentFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
        contentFont.setColor(HSSFFont.COLOR_NORMAL);

//        contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        contentStyle.setFont(contentFont);
        return contentStyle;
    }


}
